Vue.use(VueResource)

var userDetailComponent = Vue.extend({
    template: '#userDetail',
    data: function() {
        return {

        }
    },
    methods: {

    },
    props: ['user']
})

var userBoxComponent = Vue.extend({
    template: '#userBox',
    data: function() {
        return {
            page: 1,
            list: [],
            identityTab: ['普通用户', '管理员', '超级管理员']
        }
    },
    mounted: function() {
        this.getUsers(0)
    },
    methods: {
        getUsers: function(skip) {

            this.$http.get('/user/list', {
                params: {
                    page: this.page + skip
                }
            }).then(function(res) {
                if (res.data.code == 100) {
                    this.list = res.data.data.list
                    this.page += skip
                } else if (res.data.code == 112) {
                    alert('已经是最后一页了')
                } else {
                    alert(res.data.msg)
                }
            })

        },
        delUser: function(user, index) {
            this.$http.delete('/admin/user/id/' + user.id).then(function(res) {
                if (res.data.code == 100) {
                    $$.alert('删除成功')
                    this.list.splice(index, 1)
                } else {
                    $$.alert(res.data.msg)
                }
            })
        },
        editUser: function(user) {

        }

    },
    components: {
        'user-detail-component': userDetailComponent
    }
})

var adminerComponent = Vue.extend({
    template: '#adminerList',
    data: function() {
        return {
            list: []
        }
    },
    methods: {

    },
    mounted: function() {
        this.$http.get('/admin/adminer').then(function(res) {
            if (res.data.code == 100) {
                this.list = res.data.data.list
            } else {
                console.log(res.data.msg)
            }
        })
    }
})

var applyComponent = Vue.extend({
    template: '#applyList',
    data: function() {
        return {
            page: 1,
            list: []
        }
    },
    methods: {
        getApplyList: function(skip) {
            this.$http.get('/admin/apply', {
                params: {
                    page: this.page + skip
                }
            }).then(function(res) {
                if (res.data.code == 100) {
                    this.list = res.data.data.list
                    this.page += skip
                } else if (res.data.code == 112) {
                    alert('已经是最后一页了')
                } else {
                    console.log(res.data.msg)
                }
            })
        },
        handlerApply: function(user, access) {
            this.$http.put('/admin/apply/id/' + user.id, {
                access: access
            }).then(function(res) {
                if (res.data.code == 100) {
                    alert('操作成功')

                } else {
                    alert(res.data.msg)
                }
            })
        }

    },
    mounted: function() {
        this.getApplyList(0)
    }
})

var adminManageComponent = Vue.extend({
    template: '#adminManage',
    data: function() {
        return {
            tabIndex: 0
        }
    },
    methods: {

    },
    components: {
        'adminer-component': adminerComponent,
        'apply-component': applyComponent
    }
})